package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.basic.service.UserService;
import com.minimalist.common.entity.lindi.vo.tradeapplicationoperator.LindiTradeApplicationOperatorVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationOperatorFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_交易申请操作管理")
@RequestMapping("/basic/lindi/tradeApplicationOperator")
public class LindiTradeApplicationOperatorController{

    @Autowired
    private LindiTradeApplicationOperatorFeignClient feignClient;

    @Autowired
    private UserService userService;


    @PostMapping("/addTradeApplicationOperator")
    @SaCheckPermission("lindi:tradeApplicationOperator:add")
    @Operation(summary = "添加交易申请操作")
    public R<?> addTradeApplicationOperator(
            @RequestBody @Validated(Add.class)
            LindiTradeApplicationOperatorVO operatorVO){
        operatorVO.setCreateId(StpUtil.getLoginIdAsLong());
        operatorVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        operatorVO.setCreateTime(now);
        operatorVO.setUpdateTime(now);
        return feignClient.addTradeApplicationOperator(operatorVO);
    }

    @DeleteMapping("/deleteTradeApplicationOperatorById")
    @SaCheckPermission("lindi:tradeApplicationOperator:delete")
    @Operation(summary = "删除交易申请操作 -> 根据操作ID删除")
    public R<?> deleteTradeApplicationOperatorByTradeApplicationOperatorId(
            @RequestParam("tradeApplicationOperatorId")
            @NotNull(message = "交易申请操作ID不能为空")
            @Parameter(name = "tradeApplicationOperatorId", required = true, description = "交易申请操作ID")
            Long tradeApplicationOperatorId){
        return feignClient.deleteTradeApplicationOperatorByTradeApplicationOperatorId(
                tradeApplicationOperatorId);
    }

    @PutMapping("/updateTradeApplicationOperator")
    @SaCheckPermission("lindi:tradeApplicationOperator:update")
    @Operation(summary = "修改交易申请操作")
    public R<?> updateTradeApplicationOperatorByTradeApplicationOperatorId(
            @RequestBody @Validated(Update.class)
            LindiTradeApplicationOperatorVO operatorVO){
        operatorVO.setUpdateId(StpUtil.getLoginIdAsLong());
        operatorVO.setUpdateTime(LocalDateTime.now());
        return feignClient.updateTradeApplicationOperatorByTradeApplicationOperatorId(
                operatorVO);
    }

    @GetMapping("/getTradeApplicationOperatorsByTradeApplicationId/{tradeApplicationId}")
    @SaCheckPermission("lindi:tradeApplicationOperator:get")
    @Operation(summary = "根据交易申请ID查询操作记录")
    public R<List<LindiTradeApplicationOperatorVO>>
    getTradeApplicationOperatorsByTradeApplicationId(
            @PathVariable(value = "tradeApplicationId")
            @NotNull(message = "交易申请ID不能为空")
            @Parameter(name = "tradeApplicationId", description = "交易申请ID", required = true)
            Long tradeApplicationId){
        R<List<LindiTradeApplicationOperatorVO>> result =
                feignClient.getTradeApplicationOperatorsByTradeApplicationId(
                        tradeApplicationId);
        List<LindiTradeApplicationOperatorVO> data = result.getData();
        data.forEach(item ->
                item.setCreateName(userService.getUserByUserId(item.getCreateId())
                        .getUsername()));
        result.setData(data);
        return result;
    }
}